{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Find the Divisibility Array of a String"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #math #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #数学 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: divisibilityArray"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #找出字符串的可整除数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个下标从 <strong>0</strong> 开始的字符串 <code>word</code> ，长度为 <code>n</code> ，由从 <code>0</code> 到 <code>9</code> 的数字组成。另给你一个正整数 <code>m</code> 。</p>\n",
    "\n",
    "<p><code>word</code> 的 <strong>可整除数组</strong> <code>div</code>&nbsp; 是一个长度为 <code>n</code> 的整数数组，并满足：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>如果 <code>word[0,...,i]</code> 所表示的 <strong>数值</strong> 能被 <code>m</code> 整除，<code>div[i] = 1</code></li>\n",
    "\t<li>否则，<code>div[i] = 0</code></li>\n",
    "</ul>\n",
    "\n",
    "<p>返回<em> </em><code>word</code> 的可整除数组。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>word = \"998244353\", m = 3\n",
    "<strong>输出：</strong>[1,1,0,0,0,1,1,0,0]\n",
    "<strong>解释：</strong>仅有 4 个前缀可以被 3 整除：\"9\"、\"99\"、\"998244\" 和 \"9982443\" 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>word = \"1010\", m = 10\n",
    "<strong>输出：</strong>[0,1,0,1]\n",
    "<strong>解释：</strong>仅有 2 个前缀可以被 10 整除：\"10\" 和 \"1010\" 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>\n",
    "\t<li><code>word.length == n</code></li>\n",
    "\t<li><code>word</code> 由数字 <code>0</code> 到 <code>9</code> 组成</li>\n",
    "\t<li><code>1 &lt;= m &lt;= 10<sup>9</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [find-the-divisibility-array-of-a-string](https://leetcode.cn/problems/find-the-divisibility-array-of-a-string/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [find-the-divisibility-array-of-a-string](https://leetcode.cn/problems/find-the-divisibility-array-of-a-string/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"998244353\"\\n3', '\"1010\"\\n10']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        n = len(word)\n",
    "        res = [0 for _ in range(n)]\n",
    "        cursum = 0\n",
    "        for i, c in enumerate(word):\n",
    "            x = int(c)\n",
    "            cursum = cursum * 10 + x\n",
    "            cursum %= m\n",
    "            if cursum % m == 0:\n",
    "                res[i] = 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        ret = []\n",
    "        k = 0\n",
    "        for x in word:\n",
    "            k = k * 10 + int(x)\n",
    "            k %= m\n",
    "            ret.append(1 if k == 0 else 0)\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        n=len(word)\n",
    "        div=[0]*n\n",
    "        cr=0\n",
    "        print(n)\n",
    "        for i in range(n):\n",
    "            cr=cr*10+int(word[i])\n",
    "            cr=cr%m\n",
    "            if cr%m==0:\n",
    "                div[i]=1\n",
    "        return div"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        ans = []\n",
    "        pre = 0\n",
    "        for w in word:\n",
    "            pre = pre*10+int(w)\n",
    "            pre %= m\n",
    "            ans.append(1 if pre==0 else 0)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        n=len(word)\n",
    "        ans=[0]*n\n",
    "        num=0\n",
    "        for i in range(n):\n",
    "            c=int(word[i])\n",
    "            num = num*10 + c\n",
    "            num %= m\n",
    "            if num%m==0:\n",
    "                ans[i]=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        ans = [0] * len(word)\n",
    "        s = 0\n",
    "        for i, c in enumerate(word):\n",
    "            s = (s * 10 + int(c)) % m\n",
    "            if s == 0:\n",
    "                ans[i] = 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:        \n",
    "        ans, x = [], 0\n",
    "        for c in word:\n",
    "            x = (x * 10 + int(c)) % m\n",
    "            ans.append(0 if x else 1)            \n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, s: str, m: int) -> List[int]:\n",
    "        n=len(s)\n",
    "        c=0\n",
    "        ans=[]\n",
    "        for i,j in enumerate(s):\n",
    "            c=(c*10%m+(ord(j)-ord('0'))%m)%m\n",
    "            ans.append(1 if c==0 else 0)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        ans = []\n",
    "        s = 0\n",
    "        for num in word:\n",
    "            s = (s * 10 + int(num)) % m\n",
    "            if s == 0:\n",
    "                ans.append(1)\n",
    "            else:\n",
    "                ans.append(0)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        n=len(word)\n",
    "        ans=[0]*n\n",
    "        num=0\n",
    "        for i in range(n):\n",
    "            c=int(word[i])\n",
    "            num = num*10 + c\n",
    "            num %= m\n",
    "            if num%m==0:\n",
    "                ans[i]=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        pre = 0\n",
    "        rs = []\n",
    "        for char in word:\n",
    "            pre = 10 * pre + int(char)\n",
    "            pre = pre % m\n",
    "            if pre == 0:\n",
    "                rs.append(1)\n",
    "            else:\n",
    "                rs.append(0)\n",
    "        return rs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        ans = []\n",
    "        s = 0\n",
    "        for num in word:\n",
    "            s = s * 10 + int(num)\n",
    "            if s % m == 0:\n",
    "                s = 0\n",
    "                ans.append(1)\n",
    "            else:\n",
    "                s %= m\n",
    "                ans.append(0)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        n = len(word)\n",
    "        arr = [0] * n\n",
    "        t = 0 \n",
    "        for i in range(n):\n",
    "            t = (int(word[i])+t*10)%m\n",
    "            #print(t,word[i])\n",
    "            if t==0:\n",
    "                arr[i] = 1\n",
    "        return arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        div=[]\n",
    "        res=0\n",
    "        for i in range(0,len(word)):\n",
    "            res=(res*10+int(word[i]))%m\n",
    "            if res==0:\n",
    "                div.append(1)\n",
    "            else:\n",
    "                div.append(0)\n",
    "        return div\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        w = [0]*len(word)\n",
    "        s = 0\n",
    "        for i in range(len(word)):\n",
    "            s = (s*10%m + int(word[i])%m)%m\n",
    "            if s == 0:\n",
    "                w[i] = 1\n",
    "        return w"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        n = len(word)\n",
    "        res = [0] * n\n",
    "        r = 0\n",
    "        for i in range(n):\n",
    "            r = (r*10 + int(word[i])) % m\n",
    "            if r == 0:\n",
    "                res[i] = 1\n",
    "            \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        num = 0\n",
    "        n = len(word)\n",
    "        ans = [0] * n\n",
    "        for i in range(n):\n",
    "            num = (num * 10 + int(word[i])) % m\n",
    "            ans[i] += num % m == 0\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        r = 0\n",
    "        res = [0]*len(word)\n",
    "        for i, c in enumerate(word):\n",
    "            c = int(c)\n",
    "            _, r = divmod(r*10+c, m)\n",
    "            if not r:\n",
    "                res[i] = 1\n",
    "        return res\n",
    "\n",
    "\n",
    "        # (n*m+r) * 10 + c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        ans = []\n",
    "        cnt = 0\n",
    "        for i in map(int, word):\n",
    "            cnt = (cnt * 10 + i) % m\n",
    "            ans.append(1 if cnt == 0 else 0)\n",
    "        return ans\n",
    "            \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        intMap = {\n",
    "            '0': 0,\n",
    "            '1': 1,\n",
    "            '2': 2,\n",
    "            '3': 3,\n",
    "            '4': 4,\n",
    "            '5': 5,\n",
    "            '6': 6,\n",
    "            '7': 7,\n",
    "            '8': 8,\n",
    "            '9': 9\n",
    "        }\n",
    "        s = len(str(m))\n",
    "        n = len(word)\n",
    "        div = [0] * n\n",
    "        q = 0\n",
    "        for i in range(n):\n",
    "            q = (q * 10 + intMap[word[i]]) % m\n",
    "            if q == 0:\n",
    "                div[i] = 1\n",
    "            i += 1\n",
    "\n",
    "        return div\n",
    "            \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        # 如果一个数能被m整除，这个数乘以10的幂之后依然能被整除，因此我们只关注新加入的数和前一个数的余数\n",
    "        ans = []\n",
    "        cnt = 0\n",
    "        for i in map(int, word):\n",
    "            cnt = (cnt * 10 + i) % m\n",
    "            ans.append(1 if cnt == 0 else 0)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        # 如果一个数能被m整除，这个数乘以10的幂之后依然能被整除，因此我们只关注新加入的数和前一个数的余数\n",
    "        ans = []\n",
    "        cnt = 0\n",
    "        for i in map(int, word):\n",
    "            cnt = (cnt * 10 + i) % m\n",
    "            ans.append(1 if cnt == 0 else 0)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        intMap = {\n",
    "            '0': 0,\n",
    "            '1': 1,\n",
    "            '2': 2,\n",
    "            '3': 3,\n",
    "            '4': 4,\n",
    "            '5': 5,\n",
    "            '6': 6,\n",
    "            '7': 7,\n",
    "            '8': 8,\n",
    "            '9': 9\n",
    "        }\n",
    "        n = len(word)\n",
    "        div = [0] * n\n",
    "        q = 0\n",
    "        for i in range(n):\n",
    "            q = (q * 10 + intMap[word[i]]) % m\n",
    "            if q == 0:\n",
    "                div[i] = 1\n",
    "            i += 1\n",
    "\n",
    "        return div\n",
    "            \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        # 如果一个数能被m整除，这个数乘以10的幂之后依然能被整除，因此我们只关注新加入的数和前一个数的余数\n",
    "        ans = []\n",
    "        cnt = 0\n",
    "        for i in map(int, word):\n",
    "            cnt = (cnt * 10 + i) % m\n",
    "            ans.append(1 if cnt == 0 else 0)\n",
    "        return ans\n",
    "            \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        # 如果一个数能被m整除，这个数乘以10的幂之后依然能被整除，因此我们只关注新加入的数和前一个数的余数\n",
    "        ans = []\n",
    "        cnt = 0\n",
    "        for i in map(int, word):\n",
    "            cnt = (cnt * 10 + i) % m\n",
    "            ans.append(1 if cnt == 0 else 0)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "\n",
    "\n",
    "        ans = []\n",
    "\n",
    "        mod = int(word[0]) % m \n",
    "\n",
    "        if mod == 0:\n",
    "            ans.append(1)\n",
    "        else:\n",
    "            ans.append(0)\n",
    "\n",
    "        \n",
    "        for i in range(1, len(word)):\n",
    "            mod = (mod * 10 + int(word[i])) % m\n",
    "\n",
    "            \n",
    "            if mod == 0:\n",
    "                ans.append(1)\n",
    "            else:\n",
    "                ans.append(0)\n",
    "\n",
    "\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        # 如果一个数能被m整除，这个数乘以10的幂之后依然能被整除，因此我们只关注新加入的数和前一个数的余数\n",
    "        ans = []\n",
    "        cnt = 0\n",
    "        for i in map(int, word):\n",
    "            cnt = (cnt * 10 + i) % m\n",
    "            ans.append(1 if cnt == 0 else 0)\n",
    "        return ans\n",
    "            \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        intMap = {\n",
    "            '0': 0,\n",
    "            '1': 1,\n",
    "            '2': 2,\n",
    "            '3': 3,\n",
    "            '4': 4,\n",
    "            '5': 5,\n",
    "            '6': 6,\n",
    "            '7': 7,\n",
    "            '8': 8,\n",
    "            '9': 9\n",
    "        }\n",
    "        n = len(word)\n",
    "        div = [0] * n\n",
    "        q = 0\n",
    "        for i in range(n):\n",
    "            q = (q * 10 + intMap[word[i]]) % m\n",
    "            if q == 0:\n",
    "                div[i] = 1\n",
    "            i += 1\n",
    "\n",
    "        return div\n",
    "            \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        intMap = {\n",
    "            '0': 0,\n",
    "            '1': 1,\n",
    "            '2': 2,\n",
    "            '3': 3,\n",
    "            '4': 4,\n",
    "            '5': 5,\n",
    "            '6': 6,\n",
    "            '7': 7,\n",
    "            '8': 8,\n",
    "            '9': 9\n",
    "        }\n",
    "        n = len(word)\n",
    "        div = [0] * n\n",
    "        q = 0\n",
    "        for i in range(n):\n",
    "            q = (q * 10 + intMap[word[i]]) % m\n",
    "            if q == 0:\n",
    "                div[i] = 1\n",
    "            i += 1\n",
    "\n",
    "        return div\n",
    "            \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        ans = []\n",
    "        cnt = 0\n",
    "        for i in map(int, word):\n",
    "            cnt = (cnt * 10 + i) % m\n",
    "            ans.append(1 if cnt == 0 else 0)\n",
    "        return ans\n",
    "            \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        n = len(word)\n",
    "        num = 0\n",
    "        div = list()\n",
    "        for i in range(n):\n",
    "            num = (10 * num + int(word[i])) % m\n",
    "            if num % m == 0:\n",
    "                div.append(1)\n",
    "            else:\n",
    "                div.append(0)\n",
    "        return div"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        ans=[]\n",
    "        x=0\n",
    "        for i in map(int,word):\n",
    "            x=(x*10+i)%m\n",
    "            ans.append(int(x==0))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        r = 0\n",
    "        res = []\n",
    "        for w in word:\n",
    "            r = (r * 10 + int(w)) % m\n",
    "            res.append(1 if r == 0 else 0)\n",
    "        return res "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "\n",
    "        res = []\n",
    "        x = 0\n",
    "        for d in word:\n",
    "            x = (x * 10 + int(d)) % m\n",
    "            res.append(1 if x == 0 else 0)\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        x = 0\n",
    "        ans = []\n",
    "        for d in map(int, word):\n",
    "            x = (x*10+d)%m\n",
    "            ans.append(int(x==0))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        # 如果一个数能被m整除，这个数乘以10的幂之后依然能被整除，因此我们只关注新加入的数和前一个数的余数\n",
    "        ans = []\n",
    "        cnt = 0\n",
    "        for i in map(int, word):\n",
    "            cnt = (cnt * 10 + i) % m\n",
    "            if cnt == 0:\n",
    "                ans.append(1)\n",
    "            else:\n",
    "                ans.append(0)\n",
    "        return ans\n",
    "            \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        ans, x = [], 0\n",
    "        for d in map(int, word):\n",
    "            x = (x * 10 + d) % m\n",
    "            ans.append(int(x == 0))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        a=[]\n",
    "        b=0\n",
    "        for i in map(int,word):\n",
    "            b=(b*10+i)%m\n",
    "            a.append(int(b==0))\n",
    "        return a\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        ans ,x=[],0\n",
    "        for d in map(int,word):\n",
    "            x=(x*10+d)%m\n",
    "            ans.append(int(x==0))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        ans, x = [], 0\n",
    "        for d in map(int, word):\n",
    "            x = (x * 10 + d) % m\n",
    "            ans.append(int(x == 0))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        ans = []\n",
    "        s = 0\n",
    "        for num in word:\n",
    "            s = (s * 10 + int(num)) % m\n",
    "            if s % m == 0:\n",
    "                ans.append(1)\n",
    "            else:\n",
    "                ans.append(0)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        # 如果一个数能被m整除，这个数乘以10的幂之后依然能被整除，因此我们只关注新加入的数和前一个数的余数\n",
    "        ans = []\n",
    "        cnt = 0\n",
    "        for i in map(int, word):\n",
    "            cnt = (cnt * 10 + i) % m\n",
    "            ans.append(int(cnt == 0))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        n = len(word)\n",
    "        num = 0\n",
    "        ans = []\n",
    "        for c in word:\n",
    "            num = (num*10+int(c))%m\n",
    "            ans.append(1 if num==0 else 0)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        # 如果一个数能被m整除，这个数乘以10的幂之后依然能被整除，因此我们只关注新加入的数和前一个数的余数\n",
    "        ans = []\n",
    "        cnt = 0\n",
    "        for i in map(int, word):\n",
    "            cnt = (cnt * 10 + i) % m\n",
    "            ans.append(int(cnt == 0))\n",
    "        return ans\n",
    "            \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "\n",
    "\n",
    "        ans = []\n",
    "\n",
    "        mod = int(word[0]) % m \n",
    "\n",
    "        if mod == 0:\n",
    "            ans.append(1)\n",
    "        else:\n",
    "            ans.append(0)\n",
    "\n",
    "        \n",
    "        for i in range(1, len(word)):\n",
    "            mod = ((mod * (10 % m)) % m + int(word[i]) % m) % m\n",
    "\n",
    "            \n",
    "            if mod == 0:\n",
    "                ans.append(1)\n",
    "            else:\n",
    "                ans.append(0)\n",
    "\n",
    "\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        ans,x=[],0\n",
    "        for d in map(int,word):\n",
    "            x=(x*10+d)%m\n",
    "            ans.append(int(x==0))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        ans = []\n",
    "        x = 0\n",
    "        for d in map(int, word):\n",
    "            x = (x * 10 + d) % m\n",
    "            ans.append(int(x == 0))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        ans, x = [], 0\n",
    "        for d in map(int, word) :\n",
    "            x = (x * 10  + d) % m\n",
    "            ans.append(int(x == 0))\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        res=0\n",
    "        ans=[]\n",
    "        for i in range(len(word)):\n",
    "            res = (res * 10 + int(word[i])) % m\n",
    "            if res == 0:\n",
    "                ans.append(1)\n",
    "            else:\n",
    "                ans.append(0)\n",
    "        return ans\n",
    "\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        ans = []\n",
    "        prv = 0\n",
    "        for x in word:\n",
    "            prv = (prv * 10 + int(x)) % m\n",
    "            ans.append(int(prv == 0))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "\n",
    "class Solution:\n",
    "    def divisibilityArray(self, word: str, m: int) -> List[int]:\n",
    "        # 初始化结果列表\n",
    "        res = []\n",
    "        # 初始化前缀数字的值\n",
    "        num = 0\n",
    "        # 遍历每个字符\n",
    "        for ch in word:\n",
    "            # 更新前缀数字的值并对m取模\n",
    "            num = (num * 10 + int(ch)) % m\n",
    "            # 判断当前的数字是否可以被m整除，并更新结果列表\n",
    "            res.append(1 if num == 0 else 0)\n",
    "        return res\n",
    "\n",
    "# 示例测试\n",
    "solution = Solution()\n",
    "print(solution.divisibilityArray(\"998244353\", 3))  # [1,1,0,0,0,1,1,0,0]\n",
    "print(solution.divisibilityArray(\"1010\", 10))  # [0,1,0,1]\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
